import {
	createApp
} from 'vue'
import App from './App.vue'
//引入vuex
import store from './store/index.js'
import {
	createI18n
} from 'vue-i18n'
import zh from '@/i18n/zh.js'
import en from '@/i18n/en.js'
import pt from '@/i18n/pt.js'
import themeColors from '@/util/themeColors.js'
import * as utils from '@/util/utils.js'
import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
import cuCustom from '@/components/colorui/components/cu-custom.vue'
import * as commonSystemDictionariesApi from '@/api/common/commonSystemDictionariesApi.js'
import * as commonCompanyDictionariesApi from '@/api/common/commonCompanyDictionariesApi.js'
import * as iotCompanyConfigDevice from '@/api/iot/iotCompanyConfigDevice.js'
import * as companyApi from '@/api/iot/iotCompanyApi.js'
import {
	getSystemInfo
} from '@/api/iot/iotSystemApi.js'
import * as storage from '@/util/storage.js'

getSystemInfo({}).then(res => {
	const {
		data
	} = res
	if (data) {
		storage.setWebSocketUrl(data.wsUrl)
	}
})


/**
 * 获取公司信息
 */
let promise;
//#ifdef MP-WEIXIN
const appid = wx.getAccountInfoSync().miniProgram.appId;
promise = companyApi.getCompanyWithoutToken({
	wxAppAppId: appid
})
//#endif
//#ifdef H5
const param = new utils.urlSearch();
const companyId = param.companyId
if (companyId != null) {
	promise = companyApi.getCompanyWithoutToken({
		companyId: companyId
	})
} else {
	promise = companyApi.getCompanyWithoutToken({
		domainName: window.location.hostname
	})
}
//#endif
//#ifdef APP-PLUS
promise = companyApi.getCompanyWithoutToken({
	domainName: myConfig.app.domainName
})
//#endif

promise.then((res) => {
	const {
		companyId,
		companyName,
		iconUrl,
		language,
		shortCompanyName,
		enableRegister,
		enablePhoneRegister,
		enableDemoAccount,
		lat,
		lng
	} = res.data

	if (!storage.getLanguage()) {
		storage.setLanguage(language)
	}

	storage.setCompanyId(companyId)
	storage.setCompanyIconUrl(iconUrl)
	storage.setShortCompanyName(shortCompanyName)
	storage.setEnableRegister(enableRegister)
	storage.setEnablePhoneRegister(enablePhoneRegister)
	storage.setEnableDemoAccount(enableDemoAccount)
	storage.setLat(lat)
	storage.setLng(lng)

	commonCompanyDictionariesApi.listCompanyDictionaries({
		companyId: companyId
	}).then(res => {
		const {
			data
		} = res
		if (data) {
			data.forEach(item => {
				switch (item.language) {
					case 'zh':
						zh[item.keyName] = item.value
						break
					case 'en':
						en[item.keyName] = item.value + ' '
						break
					case 'pt':
						pt[item.keyName] = item.value + ' '
						break
				}
			})
		}
	})

	iotCompanyConfigDevice.getCompanyConfigDevice({
		companyId: companyId
	}).then(res => {
		const {
			data
		} = res
		if (data) {
			storage.setEnableServerLinkage(data.enableServerLinkage)
		}
	})
})

async function listAllSystemDictionaries() {
	await commonSystemDictionariesApi.listAllSystemDictionaries({}).then(res => {
		const {
			data
		} = res
		if (data) {
			data.forEach(item => {
				switch (item.language) {
					case 'zh':
						zh[item.keyName] = item.value
						break
					case 'en':
						en[item.keyName] = item.value + ' '
						break
					case 'pt':
						pt[item.keyName] = item.value + ' '
						break
				}
			})
		}
	})
}

listAllSystemDictionaries()

/**
 * 语言
 */
const language = storage.getLanguage() || 'zh'
storage.setLanguage(language)
const i18n = createI18n({
	locale: language, // 默认选择的语言
	fallbackLocale: 'zh', //没有英文的时候默认中文语言
	silentFallbackWarn: true, //抑制警告
	messages: {
		zh: zh,
		en: en,
		pt: pt
	}
})




/**
 * 不要删 会报 TypeError: Cannot read properties of undefined (reading 'meta')
 */
// App.mpType = 'app'

const app = createApp(App)
// 注册全局组件
app.component('cu-custom', cuCustom)
app.component('mescroll-body', MescrollBody)

app.use(i18n)
app.use(store)
app.mount('#app')
